﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

using ISS.CaseStudy.BLL;
using ISS.CaseStudy.Entities;

namespace CaseStudy.AdminPortal
{
    public partial class CaseStudyManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            loginInfo.Login();

            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void gvCaseStudyInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "edit")
            {
                Response.Redirect("CaseStudyInfoDetail.aspx?id=" + e.CommandArgument.ToString() + "&mode=edit");
            }
            else if (e.CommandName.ToLower() == "delete")
            {
                CaseStudyBLL.Delete(e.CommandArgument.ToString());
                CaseStudyRelatedInformationBLL.DeleteByCaseStudyId(e.CommandArgument.ToString());

                BindData();
                Response.Redirect(this.Request.Url.ToString());
            }
            else if (e.CommandName.ToLower() == "view")
            {
                Response.Redirect("CaseStudyInfoDetail.aspx?id=" + e.CommandArgument.ToString() + "&mode=view");
            }
        }

        protected void gvCaseStudyInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvCaseStudyInfo.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void imgBtnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("CaseStudyInfoDetail.aspx");
        }

        private void BindData()
        {
            DataTable dataTable = CaseStudyBLL.GetAll().Tables[0];

            foreach (DataRow row in dataTable.Rows)
            {
                row["Title"] = HttpUtility.HtmlDecode(row["Title"].ToString());
                row["Description"] = HttpUtility.HtmlDecode(row["Description"].ToString());
                row["FileName"] = HttpUtility.HtmlDecode(row["FileName"].ToString());
                row["Capability"] = HttpUtility.HtmlDecode(row["Capability"].ToString());
                row["Quote"] = HttpUtility.HtmlDecode(row["Quote"].ToString());
                row["Location"] = HttpUtility.HtmlDecode(row["Location"].ToString());
                row["OrganizationProfile"] = HttpUtility.HtmlDecode(row["OrganizationProfile"].ToString());
                row["CountryAndRegion"] = HttpUtility.HtmlDecode(row["CountryAndRegion"].ToString());
                row["OrganizationSize"] = HttpUtility.HtmlDecode(row["OrganizationSize"].ToString());

                string industryId = row["Industry"].ToString();
                IndustryInfo industryTempInfo = IndustryBLL.GetInfo(industryId);
                if (industryTempInfo != null)
                {
                    row["Industry"] = HttpUtility.HtmlDecode(industryTempInfo.Value);
                }
                else
                {
                    row["Industry"] = "";
                }

                string clientId = row["Client"].ToString();
                ClientInfo clientTempInfo = ClientBLL.GetInfo(clientId);
                if (clientTempInfo != null)
                {
                    row["Client"] = HttpUtility.HtmlDecode(clientTempInfo.Value);
                }
                else
                {
                    row["Client"] = "";
                }

                string serviceId = row["Service"].ToString();
                ServiceInfo serviceTempInfo = ServiceBLL.GetInfo(serviceId);
                if (serviceTempInfo != null)
                {
                    row["Service"] = HttpUtility.HtmlDecode(serviceTempInfo.Value);
                }
                else
                {
                    row["Service"] = "";
                }
            }

            gvCaseStudyInfo.DataSource = dataTable.DataSet;
            gvCaseStudyInfo.DataBind();
        }
    }
}
